#Actual HW#

2 + 2
print(2 + 2)


#Basic calculation

2 ^ 2
2 * 2
2 / 2
2 + 2
2 - 2

x <- 2
x
x ^ x
x ^ 2
mode(x)


#Vector
x <- c(1 : 10)
x
mode(x)
x <- c("Hello" , "world" , "!")
x
mode(x)


x <- c(TRUE, TRUE, FALSE, FALSE, TRUE, FALSE, TRUE)
x
mode(x)


x <- list("R" , "12345" , FALSE)
x
mode(x)


ls()

Example1 <- seq(2 : 50)
Example2 <- log(Example1)
Example2
Example1


a <- c(1 , 2 , 3 , 4 , "Pi")
a
mode(a)

x <- c(1 : 10)
x
length(x)
y <- 10
length(y)
x + y


x <- c(1 : 10)
y <- c(0 , 1)
y
x + y
y <- c(1 , 2 , 3)
x + y #warning because length of y is not a multiple of x
log(x) + y #warning because length of y is not a multiple of x


#Vector Operation
2 + 3 * x
(2 + 3) * x
sqrt(x)
x %% 4
y <- 3 + 2i
Re ( y )
Im ( y )
x + y


b <- seq(10)
c <- rep(1 , 10)
b
c


x <- c(1 : 10)
x
x <- c(x , 11 : 15)
x

#Set directory
getwd()
setwd("C:/Users/Sunny BAIK/Dropbox/2014 spring/19-704/r")
dir.create("hw1")


paste(1 : 12)
paste(1 : 12 , collapse="+")
paste("a", 1:3)
paste(c("a" , "b") , 1:5)
paste(c("a" , "b") , 1:4 , c("x" , "y" , "z"))


s <- 1 : 10 #another way to express vector instead c()
s

#extract some values in a vector
s[1]
s[2 : 4]
s[c(3 , 5 , 6 , 9)]
x[-1 : -2] #bring factors without x[1] and x[2]
x[[1]]


x <- 1 : 10
x[c(T , T , T , T , F , F , F , F , T , F)] # bring factors when x[i]=T
x[c(T , T , T , T , F , F , F , F , T)] #shorter, then assume x[10]=T
x[c(T , T , T , T , F , F , F , F , T , F , T)] #longer with T - ADD x[11]=NA
x[c(T , T , T , T , F , F , F , F , T , F , F)] #longer with F - Don't add x[11]
x
x[x > 5]
x[(x > 5) & (x %% 2 == 0)]


s <- 1 : 10
names(s) <-paste("s" , 1:10 , sep="")
s
names(s)
names(s) <- c("s1" , "s2") #no name after s[2]
s
s["s1"]
names(s) <- paste("s" , 1 : 10, sep="")
s["s1"]
s[c("s1" , "s3")]
s[[1]]
s[["s1"]]
s[1]


s <- 1 : 10
q <- s[c(1 : 5 , 7)]
q
q <- c(s , 11)
q


x <- 1 : 10
attributes(x)
names(x) <- paste("x" , 1:10 , sep="")
x
attributes(x) #names is one of the attributes
attributes(x) <- NULL
x
attributes(x)
attr(x , "myattr") # set individual object attributes
attr(x , "myattr")<-"myattr_value"
attr(x , "myattr")
attributes(x)

#matrix

m <- c(1 : 10)
m <- matrix(m , 2 , 5) #m is matrix of 2*5
m #data entered by column first, row second
matrix(0 , 5 , 2)
matrix(NA , 5 , 2)
length(m)
mode(m)
typeof(m)
class(m)
attributes(m)
m1 <- c(1 : 10)
length(m1)
mode(m1)
typeof(m1)
class(m1)
attributes(m1)


x <- matrix(nrow=2 , ncol=3)
x
x[1 , 1] <- 1
x[1 , 2] <- 2 
x[1 , 3] <- 3 
x[2 , 1] <- 4 
x[2 , 2] <- 5 
x[2 , 3] <- 6
x
matrix(1:6 , nrow=2)
matrix(1:6 , ncol=3)
matrix(1:6 , nrow=2, byrow=TRUE) #row first
matrix(1:6 , nrow=2, byrow=FALSE) #column first
x[1 , ]
x[ , 1]

x
colnames(x) <- c("C1" , "C2" , "C3")
x
rownames(x) <- c("R1" , "R2")
x
x[ , "C1"]
x["R1" , ]



rbind(1:3 , 4:6)
cbind(1 , 1:3)
x <- matrix(1:6 , nrow=2)
x
x <- rbind(x, 7:9)
x


t(x)
x * x
x %o% x
diag(x) #matrix
diag(10) #scalar k
diag(c(1:3)) #vector
det(x)
eigen(x)
qr(x) #qr decomposition


accdata <- c(1 , 132 , 86.7, 2, 50, 50.7, 3, 32, 36, 4, 20, 27.9, 5 , 19 , 22.8 , 6 , 11 , 19.3 , 7 ,10 , 16.7 , 8 , 9 , 14.7 , 9 , 5 , 13.2)
accdata
accdata<-c(1 , 132 , 86.7 ,
           2 , 50 , 50.7 ,
           3 , 32 , 36 ,
           4 , 20 , 27.9 ,
           5 , 19 , 22.8 ,
           6 , 11 , 19.3 ,
           7 , 10 , 16.7 ,
           8 , 9 , 14.7 ,
           9 , 5 , 13.2) #block every rows before run
accdata

accdata <- matrix(accdata , 9 , 3 , byrow=TRUE)
colnames(accdata) <- c("digit" , "actual" , "expected")
accdata

chisquare <- sum(((accdata[ , 2]-accdata[ , 3])^2)/accdata[ , 3])
chisquare

a <- matrix(c(6 , 1 ,
            0 , -3 ,
            -1 , 2) , 3 , 2 , byrow=TRUE)
b <- matrix(c(4 , 2 ,
            0 , 1 ,
            -5 , -1) , 3 , 2 , byrow=TRUE)
a + b
a - b
a * b #multiply by a11b11, a12b12; a21b21;...
t(a)


a <- matrix(c(4 , 0 , 5 ,
            0 , 1 , -6 ,
            3 , 0 , 4) , 3 , 3 , byrow=TRUE)
b <- solve(a) #inverse of a
b
a %*% b #matrix multiplication (normal a*b)
b %*% a


a
a[1 , , drop=FALSE] #get the answer by one row
a[ , 1 , drop=FALSE] #get the answer by one column
a[1 , ]
a[1 , , drop=TRUE]


#List

address <- list("Sunny place" , "4733 centre ave" , "Pittsburgh" , "PA" , 15213)
address #indexing in list use [[]] instead of []


x <- list(name= "Fred", wife="Mary", no.children=3 , child.ages=c(4,7,9))
x 
y <- list(1 , "a")
y

x $ name
x $ "name"
x[["name"]]
typeof(x[[1]])


x$age <- 40 #add age in this list
x
x$wife <- NULL #delete wife in this list
x


#data frame

people <- c("Kim" , "Bob" , "Ted" , "Sue" , "Liz" , "Amanda" , "Tricia" , "John" , "Daniel" ,  "Sophia")
scores <- c(17 , 19 , 24 , 25 , 16 , 15 , 23 , 24 , 29 , 17)
people
scores
quizscores <- data.frame(people,scores)
quizscores

rm(people , scores) #remove objects
people
scores
quizscores #still has info in data frame

quizscores[2]
quizscores[1 , 2]
quizscores$scores
attach(quizscores)
scores #make a vector as row by attach()


df <- data.frame(matrix(1:20 , ncol=2))
df
df[(df$X1 > 4) & (df$X2 %% 2 == 0),] #Slicing rows


df <- data.frame(matrix(1:6 , nrow=3))
rbind(df , c(7 , 8)) #add row
cbind(df , X3=c(7 , 8 , 9)) #add column


MinWage <- data.frame(Year=numeric() , Value=numeric())
MinWage <- edit(MinWage) # use data editor- much easier to enter values
MinWage


#a <- read.csv("~.csv" , header=TRUE) read existing csv file with header
#head(a) bring first some rows
#summary(a[2 : 9]) get summary data for selected rows


#disregard missing data

x <- c(1 , 2 , 3 , 4 , 5 , 6 , NA , 8 , 9 , 10)
x
mean(x)
mean(x , na.rm=TRUE) #disregard data=NA but not delete it
